package com.hzy.oplus.hook.utils;

import android.os.Build;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;

public class EncodeUtil {

    public static String getLanguage() {
        return Utils.getApp().getResources().getConfiguration()
                .locale.getLanguage();
    }

    public static String getSysInfoString() {
        return "Android O+" +
                "|" + Build.VERSION.RELEASE +
                "|" + UrlConstants.APP_VERSION +
                "|" + Build.MODEL;
    }

    public static String encodeKey(String userId, String code, String type, String version) {
        if (TextUtils.isEmpty(userId) || TextUtils.isEmpty(code)) {
            return null;
        }
        try {
            String md5Code = MD5.getMD5_16(code);   //Code做MD5哈希，取最后16个字符
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < 4; i++) {  //userId取4位与code取2位，交织
                builder.append(userId.substring(i * 4, i * 4 + 4));
                builder.append(code.substring(i * 2, i * 2 + 2));
            }
            builder.append(userId.substring(16));   //取后16位
            builder.append(md5Code);
            builder.append(type);
            builder.append(version);
            String result = builder.toString();
            LogUtils.e("Encode:" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
